package com.bobo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bobo.domain.SysUser;
import com.bobo.domain.SysUserRole;
import com.bobo.service.SysUserRoleService;
import com.bobo.service.SysUserService;
import com.bobo.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.ArrayList;
import java.util.List;

/**
* @author THINKAPD
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2022-11-18 10:50:54
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{
    @Autowired
    SysUserRoleService sysUserRoleService;

    @Override
    public boolean bindRole(SysUserRole sysUserRole) {
        //先根据userid删除中间表
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUid,sysUserRole.getUid());
        sysUserRoleService.remove(queryWrapper);
        //再添加中间表
        Integer[] rids = sysUserRole.getRids();
        if(null!=rids &&rids.length>0){
            for (Integer rid : rids) {
                SysUserRole userRole = new SysUserRole();
                userRole.setUid(sysUserRole.getUid());
                userRole.setRid(rid);
                sysUserRoleService.save(userRole);
            }

        }
        return true;
    }


}




